package com.hy.mydesktop.client.component.factory;
import java.util.LinkedList;
import java.util.List;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hy.mydesktop.client.component.meta.AbstractComponetModelEnum;
import com.hy.mydesktop.client.component.meta.TextAreaModelEnum;
import com.hy.mydesktop.client.rpc.ServiceInvoker;
import com.hy.mydesktop.client.rpc.ServiceInvokerAsync;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;
import com.hy.mydesktop.shared.rpc.meta.GxtToSeamServiceParameter;
import com.hy.mydesktop.shared.rpc.meta.GxtToSeamServiceResult;
/**
 * 
 * <ul>
 * <li>开发作者：冬超</li>
 * <li>设计日期：2010-8-29；时间：下午04:11:07</li>
 * <li>类型名称：TextAreaFactory</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class TextAreaFactory {

	public static TextArea createTextArea(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {
		
		return configureTextArea(gxtComponentMetaNodeModel);

	}

	/**
	 * 
	 * 
	 * @param gxtComponentMetaNodeModel
	 * @return
	 */
	private static TextArea configureTextArea(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {

		
		final TextArea textArea = createDefaultTextArea();
		AbstractComponetFactory.configureAbstractComponet(textArea, gxtComponentMetaNodeModel);

		//数据加载
		if(gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.GXTTOSEAMSERVICEPARAMETERMODEL.name().toLowerCase())!=null){
			final ServiceInvokerAsync serviceInvoker = GWT.create(ServiceInvoker.class);
			final GxtToSeamServiceParameter gxtToSeamServiceParameter = textArea.getData("gxtToSeamServiceParameter");
			/*final GxtToSeamServiceParameter gxtToSeamServiceParameter = new GxtToSeamServiceParameter();
			gxtToSeamServiceParameter.setServiceComponentName("com.test.server.business.PersonService");
			gxtToSeamServiceParameter.setServiceMethodName("loadTextFieldData");
			ModelData[] methodArguments2 = new BaseModelData[1];
			methodArguments2[0] = new BaseModel();
			gxtToSeamServiceParameter.setMethodArguments(methodArguments2 );*/
			
			serviceInvoker.callService(gxtToSeamServiceParameter,
					new AsyncCallback<GxtToSeamServiceResult>() {

						@Override
						public void onSuccess(GxtToSeamServiceResult result) {
							List<String> bmBaseModels = new LinkedList<String>();
							ModelData modelData = result.getResult();
							bmBaseModels = modelData.get("listOfModelData");
							bmBaseModels.get(0);
							textArea.setValue(bmBaseModels.get(0));
							/*List<GxtComponentMetaNodeModel> bmBaseModels = new LinkedList<GxtComponentMetaNodeModel>();
							ModelData modelData = result.getResult();
							bmBaseModels = modelData.get("listOfModelData");
							for (GxtComponentMetaNodeModel gxtComponentMetaNodeModel : bmBaseModels) {
								gxtComponentMetaNodeModel.setComponentControllerEnum(
										ComponentControllerEnumConverter.getComponentControllerEnum(gxtComponentMetaNodeModel.getComponentControllerEnumIndex()));
								//System.err.println("$$$$$$$$$" + gxtComponentMetaNodeModel.getComponentControllerEnum());
							}*/
														
						}

						@Override
						public void onFailure(Throwable caught) {
							System.err.println("#### Rpc Exception is beginning ####");
							System.err.println(caught);
							System.err.println("#### Rpc Exception is over #### ");
							caught.printStackTrace();
						}
					});
			
		}
		
		if (gxtComponentMetaNodeModel.get(TextAreaModelEnum.BLANK.name()
				.toLowerCase()) != null) {
			
			Boolean blank = (Boolean) gxtComponentMetaNodeModel
					.get(TextAreaModelEnum.BLANK.name().toLowerCase());
			textArea.setAllowBlank((boolean)blank);

		} 
		if (gxtComponentMetaNodeModel.get(TextAreaModelEnum.HIGHT.name().toLowerCase()) != null) {
			Integer height = (Integer) gxtComponentMetaNodeModel
			.get(TextAreaModelEnum.HIGHT.name().toLowerCase());
			textArea.setHeight((int)height);
			
		} 
		if (gxtComponentMetaNodeModel.get(TextAreaModelEnum.LABEL.name().toLowerCase()) != null) {
			String label = (String) gxtComponentMetaNodeModel
			.get(TextAreaModelEnum.LABEL.name().toLowerCase());
			textArea.setFieldLabel(label);
			
		} 
		if (gxtComponentMetaNodeModel.get(TextAreaModelEnum.FIELDLABEL.name().toLowerCase()) != null) {
			String label = (String) gxtComponentMetaNodeModel
			.get(TextAreaModelEnum.FIELDLABEL.name().toLowerCase());
			textArea.setFieldLabel(label);
			
		} 
		if (gxtComponentMetaNodeModel.get(TextAreaModelEnum.NAME.name().toLowerCase()) != null) {
			String name = (String) gxtComponentMetaNodeModel
			.get(TextAreaModelEnum.NAME.name().toLowerCase());
			textArea.setName(name);
			
		} 
		if (gxtComponentMetaNodeModel.get(TextAreaModelEnum.VALUE.name().toLowerCase()) != null) {
			String value = (String) gxtComponentMetaNodeModel
			.get(TextAreaModelEnum.VALUE.name().toLowerCase());
			textArea.setValue(value);
			
		} 
		

		return textArea;
	}

	private static TextArea createDefaultTextArea() {

		TextArea textArea = new TextArea();
		return textArea;
	}
}
